package py.com.erpone.session.dm;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import py.com.erpone.entity.dm.FormaPago;
import py.com.erpone.entity.dm.Laboratorio;

@Stateful
@Named
@LocalBean
@ConversationScoped
public class LaboratorioBean {

	@PersistenceContext(type=PersistenceContextType.EXTENDED)
	private EntityManager em;
	
	@Inject
	private Conversation conversation;
	
	private Laboratorio laboratorio;
	
	
	public LaboratorioBean(){
		
		
	}

	public Laboratorio getLaboratorio() {
		return laboratorio;
	}
	public void setLaboratorio(Laboratorio laboratorio) {
		this.laboratorio = laboratorio;
	}



	public void iniciar(){
		if(conversation.isTransient())
			conversation.begin();
		laboratorio = new Laboratorio();
	
	}
	
    //Modificar
	public void iniciar(Laboratorio laboratorio){
		if(conversation.isTransient())
			conversation.begin();
		this.laboratorio=em.find(Laboratorio.class, laboratorio.getId());
	}
	
	
	
	
	public void guardar(){
		if(laboratorio.getId()==null)
			em.persist(laboratorio);
		else
			em.merge(laboratorio);
		if(!conversation.isTransient())
			conversation.end();
	}
	
	
	public void eliminar(Laboratorio laboratorio){
		em.remove(em.merge(laboratorio));
		if(!conversation.isTransient())
			conversation.end();
	}
	
	public String cancelar(){
		if(!conversation.isTransient())
			conversation.end();
		return "/dm/laboratorios.xhtml";
	}
	
	public void expresionesIlegales(Laboratorio l){
	Class c = l.getClass();
	Pattern p = Pattern.compile("[0-9]");
	Matcher m;
	Field[] myFields = l.getClass().getFields();
	for (Field field : myFields) {
		if(field.getType().equals(String.class)){
			try {
				Method met;
				met = c.getMethod(field.getName(), null);
				m = p.matcher((CharSequence) ((String)met.invoke(this, null)));
				if (!m.find())
					System.out.println("cumple la restricion");
			} catch (NoSuchMethodException | SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
	}
//	Matcher m = p.matcher((CharSequence) l.getNombre());
//	 if (!m.find())
//	     	System.out.println("cumple la restricion");
	}
	
	
}
